package com.fliad.ai.listener;

import com.fliad.ai.entity.*;
import com.fliad.ai.service.AiKnowledgeService;
import com.fliad.ai.service.AiMcpService;
import com.fliad.ai.service.AiWorkflowService;
import org.noear.snack.ONode;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventListener;
import org.noear.solon.core.util.ResourceUtil;
import org.noear.wood.DbContext;
import org.noear.wood.annotation.Db;

import java.util.Date;
import java.util.List;

/**
 * @author wyl
 * @date 2025年06月16日 21:14
 */
@Component
public class AppLoadEndEventListener implements EventListener<AppLoadEndEvent> {

    @Db
    DbContext db;
    @Inject
    AiKnowledgeService aiKnowledgeService;
    @Inject
    AiMcpService aiMcpService;
    @Inject
    AiWorkflowService aiWorkflowService;

    @Override
    public void onEvent(AppLoadEndEvent appLoadEndEvent) throws Throwable {
        // 是否初始化数据
        if (Solon.cfg().getBool("solon.app.initialData", false)) {
            List<AiLlm> aiLlmList = ONode.load(ResourceUtil.getResourceAsString(ResourceUtil.findResourceOrFile(null, "init_data/ai_llm.json"))).toObjectList(AiLlm.class);
            for (AiLlm aiLlm : aiLlmList) {
                aiLlm.setCreated(new Date());
                aiLlm.setModified(new Date());
                db.table("ai_llm").setEntity(aiLlm).insertBy("id");
            }

            List<AiKnowledge> aiKnowledgeList = ONode.load(ResourceUtil.getResourceAsString(ResourceUtil.findResourceOrFile(null, "init_data/ai_knowledge.json"))).toObjectList(AiKnowledge.class);
            for (AiKnowledge aiKnowledge : aiKnowledgeList) {
                aiKnowledge.setCreated(new Date());
                aiKnowledge.setModified(new Date());
                db.table("ai_knowledge").setEntity(aiKnowledge).insertBy("id");
            }

            List<AiDocument> aiDocumentList = ONode.load(ResourceUtil.getResourceAsString(ResourceUtil.findResourceOrFile(null, "init_data/ai_document.json"))).toObjectList(AiDocument.class);
            for (AiDocument aiDocument : aiDocumentList) {
                aiDocument.setCreated(new Date());
                aiDocument.setModified(new Date());
                db.table("ai_document").setEntity(aiDocument).insertBy("id");
            }

            List<AiMcp> aiMcpList = ONode.load(ResourceUtil.getResourceAsString(ResourceUtil.findResourceOrFile(null, "init_data/ai_mcp.json"))).toObjectList(AiMcp.class);
            for (AiMcp aiMcp : aiMcpList) {
                aiMcp.setCreated(new Date());
                aiMcp.setModified(new Date());
                db.table("ai_mcp").setEntity(aiMcp).insertBy("id");
            }

            List<AiWorkflow> aiWorkflowList = ONode.load(ResourceUtil.getResourceAsString(ResourceUtil.findResourceOrFile(null, "init_data/ai_workflow.json"))).toObjectList(AiWorkflow.class);
            for (AiWorkflow aiWorkflow : aiWorkflowList) {
                aiWorkflow.setCreated(new Date());
                aiWorkflow.setModified(new Date());
                db.table("ai_workflow").setEntity(aiWorkflow).insertBy("id");
            }
        }

        // 初始化知识库
        aiKnowledgeService.initializeKnowledge();
        // 初始化MCP服务
        aiMcpService.initializeMcpServices();
        // 初始化工作流
        // aiWorkflowService.initializeWorkflows();
    }

}
